package com.example.expenses;

import mdv.exp.items.chooser.ItemsChooserFragment;
import mdv.exp.items.list.ItemsListAdapter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;
import android.widget.TextView;

public class ItemsActivity extends FragmentActivity implements OnItemSelectedListener, OnClickListener {
	


    private static final String TAG = "ItemsActivity";
	public  static final int SUPPLIER_HEADER_TAG = 0;
	public  static final int KIND_HEADER_TAG = 1;
	private ItemsListAdapter mainAdapter;

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /**
         * Main View 
         */
        setContentView(R.layout.activity_main);
        
        /**
         * ListView
         */
        ListView list = (ListView) findViewById(R.id.items_list);
        mainAdapter  = new ItemsListAdapter(this, null, true);
        list.setAdapter(mainAdapter);
        
        
        /**
         * Header Items
         */
        //Manage Header
        TextView supplierRowHeader = (TextView)findViewById(R.id.Supplier);
        supplierRowHeader.setTag(SUPPLIER_HEADER_TAG);
        supplierRowHeader.setOnClickListener(this);
        
        TextView kindRowHeader = (TextView)findViewById(R.id.Kind);
        kindRowHeader.setTag(KIND_HEADER_TAG);
        kindRowHeader.setOnClickListener(this);
        
        

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.action_add_expense:
                AddExpenseDialog ft = new AddExpenseDialog();
                ft.show(getSupportFragmentManager(), "hello");
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }


   

	
	
	@Override
	protected void onDestroy(){
		super.onDestroy();
		mainAdapter.close();
		
	}


	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int pos,
			long id) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onNothingSelected(AdapterView<?> parent) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onClick(View v) {

		int tag = (Integer) v.getTag();
		ItemsChooserFragment supplierChooser;
		switch (tag) {
		
		case SUPPLIER_HEADER_TAG:
			supplierChooser = ItemsChooserFragment.newInstance(SUPPLIER_HEADER_TAG);
			supplierChooser.show(getSupportFragmentManager(), "Picker");
			break;
		case KIND_HEADER_TAG:
			supplierChooser = ItemsChooserFragment.newInstance(KIND_HEADER_TAG);
			supplierChooser.show(getSupportFragmentManager(), "Picker");
			break;

		default:
			break;
		}
		
		

	}

}
